package com.ruoyi.kis.controller;

import cn.hutool.http.HttpException;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult_KIS;
import com.ruoyi.kis.Vars.Constants;
import com.ruoyi.kis.annotaion.BasicAuthCheck;
import com.ruoyi.kis.domain.ZjdbdF;
import com.ruoyi.kis.service.IKisService;
import com.ruoyi.kis.util.HttpSendWmsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.SwaggerDefinition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhanglm
 * @CreateDate: 2025-03-02 15:14
 * @FullNameClass: com.ruoyi.kis.controller.KisZjdbdController
 * @Description: 直接调拨单、直接调拨单反馈
 */
@RestController
@RequestMapping("/kiszjdb")
@Component("KisZjdbd")
@Api("kis接口")
public class KisZjdbdController extends BaseController {
    @Autowired
    private IKisService kisService;

    private static final Logger LOGGER = LoggerFactory.getLogger(KisZjdbdController.class);


    /**
     * 直接调拨单
     * ERP > WMS
     */
    @ApiOperation("直接调拨单")
    @RequestMapping("/kiszjdb")
    public AjaxResult_KIS zjdbd(){
        List<ZjdbdF> zjdbdFList = kisService.queryZjdbd();
        if (zjdbdFList == null || zjdbdFList.isEmpty() || zjdbdFList.size() == 0){
            return AjaxResult_KIS.success("OK",null);
        }
//        JSON parse = JSONUtil.parse(dispatchOrderFList.get(0));
//        Map<String,Object> map = new HashMap<>();
//        map.put("FEntity",zjdbdFList.get(0));
        String s = JSONUtil.toJsonStr(zjdbdFList.get(0));
        try {
//            String msg = HttpUtil.post(Constants.TRANSFER_ORDER_URL,s);
            String msg = HttpSendWmsUtil.sendWms(Constants.TRANSFER_ORDER_URL,s);
            LOGGER.info("直接调拨单推送请求：" + s);
            LOGGER.info("直接调拨单推送结果：" + msg);
            JSONObject entries = JSONUtil.parseObj(msg);
            if(entries != null ){
//                JSONObject jsonObject = entries.getJSONObject(0);
                String fBillNo = zjdbdFList.get(0).getFBillNo();
                String msgcode = entries.get("msgcode").toString();
                String kisFlag;
                if (msgcode.equals(Constants.ZHIKU.MSGCODE_SUCCESS)){
                    kisFlag = Constants.ZHIKU.KISFLAG_SUCCESS;
                }else {
                    kisFlag = Constants.ZHIKU.KISFLAG_FAILURE;
                }
                int countUpdate = kisService.updateKisICStockBillFlag(zjdbdFList.get(0).getFInterID(),kisFlag);
                LOGGER.info("直接调拨单:{}推送状态kisFlag:{}结果更新条数：{},反馈：{}",fBillNo,kisFlag,countUpdate,msg);
            }
        }catch (HttpException e){
            LOGGER.error("直接调拨单推送失败" + e.getMessage());
        }
        return AjaxResult_KIS.success();
    }
    @ApiOperation("直接调拨单反馈")
//    @RequestMapping("/kiszjdbfeedback") 说明：调拨单不用上传
    @BasicAuthCheck
    public AjaxResult_KIS zjdbdfb(@RequestBody ZjdbdF zjdbdF){
        if(zjdbdF!= null && zjdbdF.getID().equals(Constants.ZHIKU.ZhiKu_1)){
            LOGGER.info("直接调拨单上传反馈：" + zjdbdF);
//            int updateCount = kisService.updateZjdbd(zjdbdF);
//            LOGGER.info("直接调拨单上传反馈结果：" + updateCount);
//            if (updateCount == 1){
//
//                return AjaxResult_KIS.success("success","上传完成");
//            }else{
//
//                return AjaxResult_KIS.error("error","上传失败,单据号未查询到或重复");
//            }
            Map map = kisService.updateZjdbd(zjdbdF);
            if (map.get("MSGCODE").toString().equals("0")){
                JSON parse = JSONUtil.parse(zjdbdF);
                String s =  parse.toString();
                LOGGER.info("直接调拨单上传反馈数据："+s);
                return AjaxResult_KIS.success("success","直接调拨单上传反馈成功");
            }else {
                LOGGER.error("直接调拨单上传反馈失败");
                return AjaxResult_KIS.error("error",map.get("MSG").toString());

            }
        }else{
            return AjaxResult_KIS.error("error","智库不对");
        }
    }
    @ApiOperation("直接调拨单离线后反馈")
    @RequestMapping("/zjdbdfeedbackOffline")
    @BasicAuthCheck
    public AjaxResult_KIS zjdbdLXfb(@RequestBody ZjdbdF zjdbdF){
        LOGGER.info("直接调拨单上传反馈：" + zjdbdF);
        int i = kisService.insertZjdbdFeedBack(zjdbdF);
        LOGGER.info("直接调拨单上传反馈结果：" + i);
        if (i >= 1){
            return AjaxResult_KIS.success("success","上传完成");
        }else{
            return AjaxResult_KIS.error("error","未保存成功，请提供调拨单上传操作 日期-时间查询相关问题");
        }

    }

}
